package com.msb.servlet;
import com.msb.unitil.DBUnitil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.msb.mapper.Mapper;
import sun.security.util.Debug;

/**
 * @version : 1.0
 * @Author: yangmingsong
 * @Date: 2021/2/7-02-07-22:36
 * @Description: demon
 */
@WebServlet(urlPatterns = "/Doaddgoods")
public class Doaddgoods extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
//2、获取表单数据
        List list = new ArrayList<>();
        String dataType = request.getParameter("dataType");
        list.add(dataType);
        String area = request.getParameter("area");
        list.add(dataType);
        String date = request.getParameter("date");
        list.add(date);
        String goodsName = request.getParameter("goodsName");
        list.add(goodsName);
        String price = request.getParameter("price");
        list.add(price);
        String unit = request.getParameter("unit");
        list.add(unit);
        String remark = request.getParameter("remark");
        list.add(remark);
//3、数据校验
        if (dataType == null || dataType.length() < 0) {
            request.setAttribute("msg", "数据类型有误");
            return;
        }
        if (area == null || area.length() < 0) {
            request.setAttribute("msg", "数据类型有误");
            return;
        }
        if (date == null || date.length() < 0) {
            request.setAttribute("msg", "数据类型有误");
            return;
        }
        if (goodsName == null || goodsName.length() < 0) {
            request.setAttribute("msg", "数据类型有误");
            return;
        }
        if (price == null || price.length() < 0) {
            request.setAttribute("msg", "数据类型有误");
            return;
        }
        if (unit == null || unit.length() < 0) {
            request.setAttribute("msg", "数据类型有误");
            return;
        }
        if (remark == null || remark.length() < 0) {
            request.setAttribute("msg", "数据类型有误");
            return;
        }
//4、调用DBUtils添加方法
        Mapper mapper = DBUnitil.getsqlsession().getMapper(Mapper.class);
        Integer i = mapper.insert(list);
        DBUnitil.getsqlsession().close();
//5、处理返回结果
        if (i > 0) {
            //重定向，实现页面跳转
            request.setAttribute("msg", "保存成功");
            request.getRequestDispatcher("home.jsp").forward(request,response);

        } else {
            //提示信息
            request.setAttribute("msg", "保存失败");
            //重定向实现页面跳转
            request.getRequestDispatcher("addgoods.jsp").forward(request,response);
        }
    }
}
